package com.ljf.utility.enums;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ljf.common.enums.BasicEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Getter
public enum MathFormulaEnum implements BasicEnum {

    ABS("abs", 1, 3, "abs(x)", "返回数的绝对值"),
    ACOS("acos", 1, 4,"acos(x)", "返回数的反余弦值"),
    ASIN("asin", 1, 4,"asin(x)", "返回数的反正弦值"),
    ATAN("atan", 1, 4,"atan(x)", "以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值"),
    CEIL("ceil", 1, 4,"ceil(x)", "对数进行上舍入"),
    COS("cos", 1, 3,"cos(x)", "返回数的余弦"),
    EXP("exp", 1, 3,"exp(x)", "返回 e 的指数"),
    FLOOR("floor", 1, 5,"floor(x)", "对数进行下舍入"),
    LOG("log", 1, 3,"log(x)", "返回数的自然对数（底为e）"),
    MAX("max", 2, 3,"max(x,y)", "返回 x 和 y 中的最高值"),
    MIN("min", 2, 3,"min(x,y)", "返回 x 和 y 中的最低值"),
    POW("pow", 2, 3,"pow(x,y)", "返回 x 的 y 次幂"),
    ROUND("round", 1, 5,"round(x)", "把数四舍五入为最接近的整数"),
    SIN("sin", 1, 3,"sin(x)", "返回数的正弦"),
    SQRT("sqrt", 1, 4,"sqrt(x)", "返回数的平方根"),
    TAN("tan", 1, 3,"tan(x)", "返回角的正切");

    @JsonProperty("formulaName")
    @ApiModelProperty(value = "公式名称")
    private final String formulaName;

    @JsonProperty("formulaArgCount")
    @ApiModelProperty(value = "公式参数数量")
    private final Integer formulaArgCount;

    @JsonProperty("formulaNameLength")
    @ApiModelProperty(value = "公式名称长度")
    private final Integer formulaNameLength;

    @JsonProperty("formulaExpression")
    @ApiModelProperty(value = "公式表达式")
    private final String formulaExpression;

    @JsonProperty("description")
    @ApiModelProperty(value = "公式描述")
    private final String description;

    MathFormulaEnum(String formulaName, Integer formulaArgCount, Integer formulaNameLength, String formulaExpression, String description) {
        this.formulaName = formulaName;
        this.formulaArgCount = formulaArgCount;
        this.formulaNameLength = formulaNameLength;
        this.formulaExpression = formulaExpression;
        this.description = description;
    }

    /**
     * 根据自定义公式名称，和参数数量返回匹配的枚举实体
     *
     * @param formulaName
     * @param formulaArgCount
     * @return
     */
    public static MathFormulaEnum getSelfMathFormulaEnum(String formulaName, Integer formulaArgCount) {
        for (MathFormulaEnum selfMathFormulaEnum : MathFormulaEnum.values()) {
            if (selfMathFormulaEnum.getFormulaName().equals(formulaName) && selfMathFormulaEnum.getFormulaArgCount().equals(formulaArgCount)) {
                return selfMathFormulaEnum;
            }
        }
        return null;
    }

    /**
     * 根据名称获取函数名
     *
     * @param formulaName
     * @return
     */
    public static MathFormulaEnum getSelfMathFormulaEnum(String formulaName) {
        for (MathFormulaEnum selfMathFormulaEnum : MathFormulaEnum.values()) {
            if (selfMathFormulaEnum.getFormulaName().equals(formulaName)) {
                return selfMathFormulaEnum;
            }
        }
        return null;
    }

    /**
     * 获取自定义公式的简单名称集合
     *
     * @return
     */
    public static List<String> getSelfMathFormulaNames() {
        List<String> formulaNames = new ArrayList<>();
        for (MathFormulaEnum selfMathFormulaEnum : MathFormulaEnum.values()) {
            formulaNames.add(selfMathFormulaEnum.getFormulaName());
        }
        return formulaNames;
    }

    /**
     * 获取所有的自定义函数枚举
     *
     * @return
     */
    public static List<MathFormulaEnum> getSelfMathFormulas() {
        return new ArrayList<>(Arrays.asList(MathFormulaEnum.values()));
    }

    @Override
    public Integer getCode() {
        return null;
    }

    @Override
    public String getName() {
        return formulaExpression;
    }
}
